﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IGolovkine.AoA.Tcp;
using System.Windows.Markup;
using System.IO;
using IGolovkine.AoA.Tcp.Channels.Http;
using System.Threading;
using System.Net;
using System.Diagnostics;
using System.Reflection;

namespace IGolovkine.AoA.Hosting.Console
{
    public class TcpTunnelHost
    {
        private readonly string _xamlConfigFilePath;
        public TcpTunnelHost(string xamlConfigFilePath)
        {
            _xamlConfigFilePath = xamlConfigFilePath;
        }

        public TcpTunnel Activate()
        {
            PreloadDependencies();
            TcpTunnelConfiguration configuration = (TcpTunnelConfiguration)XamlReader.Load(File.OpenRead(_xamlConfigFilePath));
            TcpTunnel tunnel = new TcpTunnel(configuration.Channel.CreateInstance());
            configuration.Routes.Subscribe(tunnel.Routes);
            return tunnel;
        }

        private static void PreloadDependencies()
        {
            string path = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            foreach (string assemblyFile in Directory.GetFiles(path, "*.dll"))
            {
                Assembly.LoadFile(assemblyFile);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TcpTunnelHost host = new TcpTunnelHost("config.xaml");
            TcpTunnel tunnel = host.Activate();
            System.Console.ReadLine();
        }
    }
}
